const http = require('http')
const qs=require('querystring')
const config=require('../config') 
const apiaddress=require('./apiaddress')


const createMember = async (ctx,next) => { //创建沙巴账户
    const userId =  ctx.session.userId
    let {coin}=ctx.request.body
    let params={coin}
    let postdata=qs.stringify(params)
    const result=new Promise((resolve,reject) => {
        let opt={
            hostname:config.aghostname,
            port: config.agport,
            path:`${apiaddress.createMember}${userId}`,
            method: "POST",
            headers:{
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': postdata.length
            }
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8')
            let resdata=''
            res.on('data',(chunk) => {
                resdata+=chunk
            })
            res.on('end',() => {
                resolve(JSON.parse(resdata))
            })
        })
        req.on('error',(e) => {
            reject(e)
        })
        req.write(postdata)
        req.end()
    })
    let data=await result
    if(data.code&&data.code!=200){
        ctx.error(data)
    }else{
        ctx.success(data)
    }
}

const sabahlogin = async (ctx) => { //默认沙巴账户登录
    const userId =  ctx.session.userId
    if(userId){
        let params={
            coin:ctx.session.coin?ctx.session.coin:'HPS'
        }
        let postdata=qs.stringify(params)
        const result = new Promise( (resolve,reject) => {
            let opt={
                hostname:config.aghostname,
                port: config.agport,
                path:`${apiaddress.sabahlogin}${userId}`,
                method: "POST",
                headers:{
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': postdata.length
                }
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    if(JSON.parse(resdata).code==200){
                        resolve(JSON.parse(resdata).data)
                    }else{
                        resolve(null)
                    }
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.write(postdata)
            req.end()
        })
        return result
    }else{
        return null
    }
}

const shabaloginajax = async (ctx,next) => { //用户切换币种登录沙巴游戏
    const userId =  ctx.session.userId
    let {coin}=ctx.request.body
    let params={coin}
    let postdata=qs.stringify(params)
    if(userId){
        const result=new Promise((resolve,reject) => {
            let opt={
                hostname:config.aghostname,
                port: config.agport,
                path:`${apiaddress.sabahlogin}${userId}`,
                method: "POST",
                headers:{
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': postdata.length
                }
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    resolve(JSON.parse(resdata))
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.write(postdata)
            req.end()
        })
        let data=await result
        if(data.code&&data.code!=200){
            ctx.error(data)
        }else{
            ctx.success(data)
        }
    }else{
        let data={
            code:200,
            data:null
        }
        ctx.success(data)
    }
}

const sabahUserBalance = async (ctx,next) => { //获取用户沙巴账户余额
    const userId =  ctx.session.userId
    let {coin}=ctx.request.body
    ctx.session.coin=coin
    let params={coin}
    let postdata=qs.stringify(params)
    const result=new Promise((resolve,reject) => {
        let opt={
            hostname:config.aghostname,
            port: config.agport,
            path:`${apiaddress.getsabahUserBalance}${userId}`,
            method: "POST",
            headers:{
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': postdata.length
            }
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8')
            let resdata=''
            res.on('data',(chunk) => {
                resdata+=chunk
            })
            res.on('end',() => {
                resolve(JSON.parse(resdata))
            })
        })
        req.on('error',(e) => {
            reject(e)
        })
        req.write(postdata)
        req.end()
    })
    let data=await result
    if(data.code&&data.code!=200){
        ctx.error(data)
    }else{
        ctx.success(data)
    }
}

const fundTransfer = async (ctx,next) => { //兑换兑出沙巴庄户
    const userId =  ctx.session.userId
    let postdata=qs.stringify(ctx.request.body)
    if(userId){
        let {coin}=ctx.request.body
        ctx.session.coin=coin
        const result=new Promise((resolve,reject) => {
            let opt={
                hostname:config.aghostname,
                port: config.agport,
                path:`${apiaddress.sabafundTransfer}${userId}`,
                method: "POST",
                headers:{
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': postdata.length
                }
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    resolve(JSON.parse(resdata))
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.write(postdata)
            req.end()
        })
        let data=await result
        if(data.code&&data.code!=200){
            ctx.error(data)
        }else{
            ctx.success(data)
        }
    }else{
        let data={
            code:200,
            data:null
        }
        ctx.success(data)
    }
}

module.exports={
    createMember,
    sabahlogin,
    shabaloginajax,
    sabahUserBalance,
    fundTransfer
}